﻿using System.Collections.Generic;
using Hidistro.Core;
using Hidistro.Entities.Commodities;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_CaiNiXiHuan : WebControl
    {
        private Database database = DatabaseFactory.CreateDatabase();
        public wnxd_CaiNiXiHuan()
        {
            base.ID = "wnxd_CaiNiXiHuan";
        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }
        public string RendHtml()
        {
            StringBuilder builder = new StringBuilder();
            DataTable info = this.GetProducts(this.productId, this.MaxNum);
            if (info == null)
            {
                this.display.hide = true;
                return null;
            }
            foreach (DataRow row in info.Rows)
            {
                builder.Append("<div class=\"right-pro\">");
                string title = string.Empty;
                string alt = string.Empty;
                string keyword = row["Meta_Keywords"].ToString();
                if (!string.IsNullOrEmpty(keyword))
                {
                    string[] arr = keyword.Split('|');
                    if (arr.Length == 3)
                    {
                        title = arr[1];
                        alt = arr[2];
                    }
                }
                builder.AppendFormat("<a href=\"{1}\"><img src=\"{0}\" title=\"{2}\" alt=\"{3}\"></a>", Globals.GetImgUrl(row["ThumbnailUrl310"].ToString()), Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), title, alt);
                builder.AppendFormat("<p><a title=\"{0}\" href=\"{1}\" style=\"color:#333333\">{0}</a>", row["ProductName"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                if (row["ProductText"] != DBNull.Value) builder.AppendFormat("&nbsp;&nbsp;<a style=\"color:#F53864\" title=\"{0}\" href=\"{1}\">{0}</a>", row["ProductText"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                builder.AppendFormat("</p><b>￥{0}</b>", Globals.FormatMoney((decimal)row["MarketPrice"]));
                builder.Append("</div>");
            }
            return builder.ToString();
        }
        private DataTable sql_call(string sql)
        {
            using (DbCommand cmd = database.GetSqlStringCommand(sql))
            using (IDataReader reader = database.ExecuteReader(cmd))
            {
                return DataHelper.ConverDataReaderToDataTable(reader);
            }
        }
        private DataTable GetProducts(int productId, int maxnum)
        {
            IList<int> browedProductList = BrowsedProductQueue.GetBrowedProductList(maxnum);
            int dtx = maxnum - browedProductList.Count;
            DataTable table1 = null, table2 = null;
            if (dtx > 0) table1 = this.sql_call(string.Format("select top {0} * from Hishop_Products where TypeId=(select TypeId from Hishop_Products where ProductId={1}) and ProductId!={1} order by newid()", maxnum, productId));
            if (browedProductList.Count > 0) table2 = this.GetRelated(browedProductList);
            DataTable newtb = null;
            if (table1 == null && table2 == null) return null;
            else if (table1 == null) newtb = table2;
            else if (table2 == null) newtb = table1;
            else
            {
                newtb = table1.Clone();
                object[] obj = new object[newtb.Columns.Count];
                for (int i = 0; i < table1.Rows.Count; i++)
                {
                    table1.Rows[i].ItemArray.CopyTo(obj, 0);
                    newtb.Rows.Add(obj);
                }
                for (int i = 0; i < table2.Rows.Count; i++)
                {
                    table2.Rows[i].ItemArray.CopyTo(obj, 0);
                    newtb.Rows.Add(obj);
                }
            }
            return newtb;
        }
        private DataTable GetRelated(IList<int> productIds)
        {
            string pids = "";
            for (int i = 0; i < productIds.Count; i++)
            {
                if (i > 0)
                {
                    pids += ",";
                }
                pids += productIds[i];
            }
            return this.sql_call(string.Format("select top {0} * from Hishop_Products where TypeId in (select TypeId from Hishop_Products where ProductId in ({1})) and ProductId not in ({1}) order by newid()", productIds.Count, pids));
        }
        public int productId { get; set; }
        public int MaxNum { get; set; }
        public wnxd_Display display { get; set; }
    }
}
